package com.lhy.goods.controller;


import com.lhy.domain.Product;
import com.lhy.entity.PageResult;
import com.lhy.entity.Result;
import com.lhy.goods.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;



import com.github.pagehelper.Page;

import java.util.List;

/**
 * 
 */
@RestController
@RequestMapping("/Product")
public class ProductController {
    @Autowired
    private ProductService iProductService;


    /**
    * 查询全部
    * @param
    * @return
    */
    @GetMapping("/selectAll")
    public Result< List<Product> > selectAll(){
        return Result.ok("查询成功",iProductService.selectAll());
    }


    /**
     * 条件查询
     * @param iProduct
     * @return
     */
    @PostMapping("/selectList")
    public Result< List<Product> > select(@RequestBody Product iProduct){
        return Result.ok("条件查询成功",iProductService.selectList(iProduct));
    }

    /**
     * 新增
     * @param iProduct
     * @return
     */
    @PostMapping("/insert")
    public Result insert(@RequestBody Product iProduct){
        iProductService.insert(iProduct);
        return Result.ok("新增成功");
    }


    /**
     * 删除
     * @param ids
     * @return
     */
    @GetMapping("/delete")
    public Result delete(@RequestParam String ids){
        iProductService.delete(ids);
        return Result.ok("删除成功");
    }


    /**
     * 修改
     * @param iProduct
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody Product iProduct){
        iProductService.update(iProduct);
        return Result.ok("修改成功");
    }

    /**
    * 分页搜索实现
    * @param page 默认值1
    * @param size 默认值10
    * @return
    */
    @GetMapping(value = "/pageAll" )
    public Result pageAll(@RequestParam(defaultValue = "1")  Integer page, @RequestParam(defaultValue = "10") Integer size){
    Page<Product> pageList = iProductService.pageAll(page, size);
        PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
        return Result.ok("查询成功",pageResult);
    }

    /**
    * 分页条件搜索实现
    * @param iProduct
    * @param page
    * @param size
    * @return
    */
    @GetMapping(value = "/pageList" )
    public Result pageList(@RequestBody Product iProduct,@RequestParam(defaultValue = "1")  Integer page, @RequestParam(defaultValue = "10") Integer size){
    Page<Product> pageList = iProductService.pageList(iProduct,page, size);
        PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
        return Result.ok("查询成功",pageResult);
    }
    /**
     * 审核
     * @param id
     * @return
     */
    @PostMapping("/audit")
    public Result audit(@RequestParam Long id,@RequestParam Integer audit){
        Integer i = iProductService.audit(id,audit);
        if (i == -1) {
            return Result.fail("审核失败");
        }
        if (i == 2){
            return Result.fail("审核拒绝");
        }
        return Result.ok("审核成功");
    }
    /**
     * 上、下架
     * @param id
     * @return
     */
    @PostMapping("/status")
    public Result status(@RequestParam Long id,@RequestParam Integer status){
        Integer i = iProductService.status(id,status);
        if (i != 1) {
            return Result.fail("上下架失败");
        }
        if (status == 0){
            return Result.ok("下架成功");
        }
        return Result.ok("上架成功");
    }


}




